package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Collectclubbean;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Collectcoursebean;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.IsCollectcoursebean;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Clubmodel;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Coursemodel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Nongfusanquan on 2017-10-16.
 */

public class ClubdetailActivtiy extends AppCompatActivity{
    private TextView clubname,clubintro,clubtel,clubcouchname;
    private Button clubdetailreturn,more,clubcoursemore;

    private ImageView clubpic;
    private ImageView collect;
    private int flag = 0;
    private String success;
    private int user_id;
    private  int club_id;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubdetail);
        initViews();
        initEvents();
        getData();
        Bundle bundle = this.getIntent().getExtras();
        club_id = bundle.getInt("c_id");
        SharedPreferences sp = this.getSharedPreferences("cache", Context.MODE_APPEND);
        user_id = Integer.parseInt(sp.getString("uid","0"));
        checkIsBeCollect(user_id,club_id);

    }
    private void initViews(){
        clubname =(TextView)findViewById(R.id.clubname);
        clubintro =(TextView)findViewById(R.id.clubdetail_intro);
        clubtel =(TextView)findViewById(R.id.clubdetail_tel);
        clubcouchname =(TextView)findViewById(R.id.clubdetail_couchname);
        clubpic =(ImageView)findViewById(R.id.clubdetail_pic);
        clubdetailreturn=(Button)findViewById(R.id.return_clubdetail);
        more =(Button)findViewById(R.id.more);
        clubcoursemore =(Button)findViewById(R.id.clubcouse_more);
        collect = (ImageView) findViewById(R.id.collect);

    }

    private void initEvents(){
        clubdetailreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
         Intent intent = new Intent();
                intent.setClass(ClubdetailActivtiy.this,CouchActivity.class);
                startActivityForResult(intent,1);
            }
        });

        clubcoursemore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ClubdetailActivtiy.this,ClubcourseActivity.class);
                startActivityForResult(intent,1);
            }
        });


        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isBeCollect(user_id,club_id);
            }
        });
    }



    private void getData() {
        Bundle bundle = this.getIntent().getExtras();
        String cname = bundle.getString("c_name");
        String phone = bundle.getString("c_phone");
        String cintro =bundle.getString("c_info");
        String cpicture = "http://10.0.2.2/yuejian14/Public/Admin/clubphoto/"+bundle.getString("c_pic");

        clubname.setText(cname);
        clubtel.setText(phone);
        clubintro.setText(cintro);


        Picasso.with(this).load(cpicture).into(clubpic);
    }

    private void checkIsBeCollect(int user_id,int club_id) {
        Clubmodel clubModel = new Clubmodel();
        Call call = clubModel.CheckClub(user_id,club_id);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Collectclubbean data = (Collectclubbean) response.body();
                success = data.getSuccess();

                if (flag == 0){
                    if(!"0".equals(success)){
                        collect.setImageResource(R.drawable.ic_collect_red);
                    }
                    else{
                        collect.setImageResource(R.drawable.ic_collect_hui);
                    }
                    flag++;
                }else {
                    if(!"0".equals(success)){
                        collect.setImageResource(R.drawable.ic_collect_red);
                        Toast.makeText(ClubdetailActivtiy.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        collect.setImageResource(R.drawable.ic_collect_hui);
                        Toast.makeText(ClubdetailActivtiy.this, "收藏失败", Toast.LENGTH_SHORT).show();
                    }
                }

            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }

    private void isBeCollect(int user_id,int club_id) {
         Clubmodel clubModel = new Clubmodel();
        Call call = clubModel.CollectClub(user_id,club_id);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Collectclubbean data = (Collectclubbean) response.body();
                success = data.getSuccess();

                if (flag == 0){
                    if(!"0".equals(success)){
                        collect.setImageResource(R.drawable.ic_collect_red);
                    }
                    else{
                        collect.setImageResource(R.drawable.ic_collect_hui);
                    }
                    flag++;
                }else {
                    if(!"0".equals(success)){
                        collect.setImageResource(R.drawable.ic_collect_red);
                        Toast.makeText(ClubdetailActivtiy.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        collect.setImageResource(R.drawable.ic_collect_hui);
                        Toast.makeText(ClubdetailActivtiy.this, "收藏失败", Toast.LENGTH_SHORT).show();
                    }
                }

            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }
}
